package com.fas.hehe.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.fas.core.BaseController;
import com.fas.core.constant.R;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.hehe.system.model.OperateLog;
import com.fas.hehe.system.model.OperateLogDetail;
import com.fas.hehe.system.service.OperateLogService;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 
 * 系统操作日志管理
 *
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午8:09:41
 * @version V1.0
 */
@RequiresAuthentication
@ControllerBind(controllerKey = "/system/operateLog", viewPath = "/system/operateLog")
public class OperateLogController extends BaseController {

	public OperateLogService operateLogService;

	// 分页查询
	@RequiresPermissions(value = { R.Authority.OperateLog.QUERY }, logical = Logical.AND)
	public void list() {
		Page<Record> recordList = operateLogService.queryPage(this.buildPageRequest());
		this.returnPage(recordList);
	}

	// 查询操作日志详细信息
	@RequiresPermissions(value = { R.Authority.OperateLog.QUERY }, logical = Logical.AND)
	public void loadLogDetailByPk() throws Exception {
		String sql = "select * from sys_operate_log_detail where log_id =?";
		List<OperateLogDetail> list = OperateLogDetail.dao.find(sql, this.getPara("logid"));
		OperateLog log = (OperateLog) this.findById(this.getPara("logid"));

		HashMap<String, Object> returnData = new HashMap<String, Object>();

		List<OperateLogDetail> newList = null;
		if (log != null && log.getLogType() == R.OperType.UPDATE.getCode()) {
			newList = new ArrayList<OperateLogDetail>();
			for (OperateLogDetail detail : list) {
				String oldValue = detail.getOldValue() + "";
				String newValue = detail.getNewValue() + "";
				if (!oldValue.equals(newValue)) {
					detail.put("changed", true);
				} else {
					detail.put("changed", false);
				}
				newList.add(detail);
			}
			list.clear();
			list.addAll(newList);
			newList.clear();
		}

		returnData.put("log", log);
		returnData.put("logItems", list);
		this.returnJsonResult(returnData);

	}

}
